package com.springweb.unit5.config;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan(basePackages={"com.springweb.unit5", "com.springweb.unit7", "com.springweb.unit9"}, excludeFilters={@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)})
@PropertySource({"classpath:/property/db.properties"})
public class RootConfig {
	@Autowired
	Environment env;
	
	@Bean("dataSource")
	public DataSource dataSource() {
		BasicDataSource dataSource = new BasicDataSource();
		
		String driverClassName = env.getProperty("jdbc.driver");
		String url = env.getProperty("jdbc.url");
		String username = env.getProperty("jdbc.username");
		String password = env.getProperty("jdbc.password");
		int initialSize = env.getProperty("jdbc.initialSize", Integer.class);
		int minIdle = env.getProperty("jdbc.minIdle", Integer.class);
		int maxIdle = env.getProperty("jdbc.maxIdle", Integer.class);
		int maxActive = env.getProperty("jdbc.maxActive", Integer.class);
		long maxWait = env.getProperty("jdbc.maxWait", Long.class);
		boolean defaultAutoCommit = env.getProperty("jdbc.defaultAutoCommit", Boolean.class);
		boolean removeAbandoned = env.getProperty("jdbc.removeAbandoned", Boolean.class);
		int removeAbandonedTimeout = env.getProperty("jdbc.removeAbandonedTimeout", Integer.class);
		boolean testWhileIdle = env.getProperty("jdbc.testWhileIdle", Boolean.class);
		long timeBetweenEvictionRunsMillis = env.getProperty("jdbc.timeBetweenEvictionRunsMillis", Long.class);
		int numTestsPerEvictionRun = env.getProperty("jdbc.numTestsPerEvictionRun", Integer.class);
		long minEvictableIdleTimeMillis = env.getProperty("jdbc.minEvictableIdleTimeMillis", Long.class);
		
		dataSource.setDriverClassName(driverClassName);
		dataSource.setUrl(url);
		dataSource.setUsername(username);
		dataSource.setPassword(password);
		dataSource.setInitialSize(initialSize);
		dataSource.setMinIdle(minIdle);
		dataSource.setMaxIdle(maxIdle);
		dataSource.setMaxActive(maxActive);
		dataSource.setMaxWait(maxWait);
		dataSource.setDefaultAutoCommit(defaultAutoCommit);
		dataSource.setRemoveAbandoned(removeAbandoned);
		dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
		dataSource.setTestWhileIdle(testWhileIdle);
		dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
		dataSource.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
		dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
		
		return dataSource;
	}
}
